package JiChu;

//生产者和消费者
public class Wait_notify {
    private int value;
    private boolean hasValue = false;

    public synchronized void produce(int v) throws InterruptedException {
        while (hasValue) wait();  // 等待消费
        value = v;
        hasValue = true;
        notify(); // 通知消费者
    }

    public synchronized int consume() throws InterruptedException {
        while (!hasValue) wait(); // 等待生产
        hasValue = false;
        notify(); // 通知生产者
        return value;
    }
}
